<?php
namespace ac\modules\member\controllers;

use Yii;
use ac\controllers\BasicController;
use app\models\Opened;
use app\modules\member\models\Member;
use app\modules\member\models\MemberWechat;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;

class RegController extends BasicController
{

    /*注册 通过 用户名、邮箱、密码*/
    public function actionReg()
    {
        // P($this->post);
        if(Yii::$app->request->isPost){
        	/*根据appid 查询comid*/


            // P($this->post);
            if($this->post['Member']['password'] != $this->post['Member']['passwordConfirm']){
                $backInfo = ErrMap::getError(10023);//返回信息
            }else{
                $member = new Member;
                if($member->addMember($this->post)){
                    $backInfo = [0, '注册会员成功'];//返回信息
                }else{
                    if($member->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $member->getErrors();//异常信息
                        if(array_key_exists("repeatLoginname", $exception)){
                            $backInfo = ErrMap::getError(10011);//返回信息
                        };
                        if(array_key_exists("repeatPhone", $exception)){
                            $backInfo = ErrMap::getError(10014);//返回信息
                        };
                    }
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*通过微信授权 为了之前通过CMS调取的函数 做个保存*/
    public function actionRegByWechat()
    {
        if(Yii::$app->request->isPost){
            // P($this->post);

            /*判断 su_ac_member_wechat 表 是否已经存在，如果存在，跳过此步骤，否则添加*/
            if(isset($this->post['Member']['openid'])){
                $wechat = MemberWechat::find()
                          ->where('wechat_openid = :openid', [':openid' => $this->post['Member']['openid']])
                          ->one();
                $pass = true;
                if(is_null($wechat)){
                    $transaction = Yii::$app->db->beginTransaction();//事物处理
                    try{
                        /*增加到 MemberWechat 表*/
                        $data['MemberWechat']['wechat_openid'] = $this->post['Member']['openid'];
                        $memberWechat = new MemberWechat;
                        if(!($id = $memberWechat->addWeChat($data))){
                             $pass = false;
                            if($memberWechat->hasErrors()){
                                $backInfo = ErrMap::getError(10005);//返回信息
                                $exception = $memberWechat->getErrors();//异常信息
                            }
                        }

                        /*增加到 member 表*/
                        if($pass){
                            $member = new Member;
                            $this->post['Member']['wechat_id'] = $id;
                            if(!($mid = $member->addMember($this->post))){
                                 $pass = false;
                                if($member->hasErrors()){
                                    $backInfo = ErrMap::getError(10005);//返回信息
                                    $exception = $member->getErrors();//异常信息
                                }
                            }
                        }

                        if($pass){
                            $backInfo = [0, '授权成功'];//返回信息
                            $transaction->commit();
                            $info = Member::find()
                                    ->select(['member_id', 'memberid', 'loginname', 'phone', 'email', 'nickname', 'sex', 'signature', 'headimgurl', '{{%member}}.wechat_id', 'comid', 'appid', 'add_time'])
                                    ->joinWith('wechat')
                                    ->where('member_id = :mid', [':mid' => $mid])
                                    ->asArray()->one();
                            // P($info);
                        }
                    }catch(\Exception $exception){
                        $transaction->rollback();
                    };
                }else{
                    /*增加最后登录时间、最后登录IP、登录总数*/
                    Member::updateAll(['lastloginip' => Yii::$app->request->userIP, 'lastlogin_time' => date('Y-m-d H:i:s', time())], 'wechat_id = :wechatid and appid = :appid', [':wechatid' => $wechat->wechat_id, ':appid' => $this->post['Member']['appid']]);
                    Member::updateAllCounters(['logintimes' => 1], 'wechat_id = :wechatid and appid = :appid', [':wechatid' => $wechat->wechat_id, ':appid' => $this->post['Member']['appid']]);
                    $backInfo = [0, '授权成功'];//返回信息
                    $info = Member::find()
                            ->select(['member_id', 'memberid', 'loginname', 'phone', 'email', 'nickname', 'sex', 'signature', 'headimgurl', '{{%member}}.wechat_id', 'comid', 'appid', 'add_time'])
                            ->joinWith('wechat')
                            ->where('{{%member}}.wechat_id = :wid and appid = :appid', [':wid' => $wechat->wechat_id, ':appid' => $this->post['Member']['appid']])
                            ->asArray()->one();
                    // P($info);
                }
                if(!is_null($info)){
                    unset($info['wechat_id']);
                    unset($info['wechat']['wechat_id']);
                }
                // P($info);
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        if(!isset($info)){
            $info = '';//登录信息 需要存入session
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo), $exception, $info) ) != 'boolean'){
            return $res;
        };
    }

    /*注册 通过 手机号*/

    /*统计*/

}
